package com.smileymedia.util;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;

import android.content.Context;
import android.os.Environment;
import android.util.Log;

import com.smileymedia.R;
import com.smileymedia.core.Constants;


/***
 * Utlity class to help us  read/write data from SDCard
 * @author greg
 *
 */
public class SDCardUtil {
	private static final String TAG = SDCardUtil.class.getSimpleName();

	/***
	 * Read file from SDCard
	 * @param context
	 * @param fileName
	 * @param isAbsolutePath
	 * @return
	 */
	public static FileInputStream readFile(Context context, String fileName, boolean isAbsolutePath){		
		File f = null;
		if(isAbsolutePath){
			f=new File(fileName);
		}else{
			f=new File(Environment.getExternalStorageDirectory() + "/" +  Constants.EXPORT_DIR + "/" + fileName);	
		}
		FileInputStream fis = null;		
		try {
			fis=new FileInputStream(f);
		} catch (FileNotFoundException e) {			
			e.printStackTrace();
		}		
		return fis;
	}

	/**
	 * Read file from SDCard into a byte[] array
	 * @param context
	 * @param fileName
	 * @param isAbsolutePath
	 * @return
	 */
	public static byte[] readBytes(Context context, String fileName, boolean isAbsolutePath){		
		File f = null;
		if(isAbsolutePath){
			f=new File(fileName);
		}else{
			f=new File(Environment.getExternalStorageDirectory() + "/" +  Constants.EXPORT_DIR + "/" + fileName);	
		}
		FileInputStream fis = null;		
		ByteArrayOutputStream baos=null;
		try {
			fis=new FileInputStream(f);			
			baos=new ByteArrayOutputStream(fis.available());
			while(fis.available()>0){
				baos.write(fis.read());
			}
		} catch (FileNotFoundException e) {			
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}		

		return baos.toByteArray();
	}

	/**
	 * Return absolute path array of all the files
	 * @param context
	 * @param directory
	 * @param filenameFilter 
	 * @return
	 */
	public static String[] listFiles(Context context, String directory, FilenameFilter filenameFilter){		
		File fDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+ "/" + Constants.EXPORT_DIR+"/");
		if(fDir.isDirectory() == false){
			return new String[0];
		}
	
		String[] fileList=fDir.list(filenameFilter);
		
		int size=fileList.length;
		String[] outList=new String[size];
		for(int i=0;i<size;i++){
			outList[i]=fDir.getAbsolutePath()+"/"+fileList[i];
		}

		return outList;
	}


	public static void mkdir(Context context, String dirName){
		Log.d(TAG, " *************** ABOUT TO Create Directory **** ");

		// first see if an SD card is even present; abort if not
		if (!Environment.MEDIA_MOUNTED.equals(Environment .getExternalStorageState())) {
			//showOutcomeDialog(R.string.export_failure_title, context.getResources().getString(R.string.export_failure_missing_sd));
			Log.d(TAG, context.getResources().getString(R.string.export_failure_missing_sd));
			return;
		}
		File f = null;
		try {
			f = new File(Environment.getExternalStorageDirectory() + "/" + Constants.EXPORT_DIR+"/"+dirName);
			f.mkdirs();
		}catch(Exception e){
			Log.e(TAG, context.getResources().getString(R.string.export_failure_io_error));
		}
	}

	public static void writeFile(Context context, String fileName, byte[] data){
		Log.d(TAG, " *************** ABOUT TO MAKE A SAVE**** ");

		// first see if an SD card is even present; abort if not
		if (!Environment.MEDIA_MOUNTED.equals(Environment .getExternalStorageState())) {
			//showOutcomeDialog(R.string.export_failure_title, context.getResources().getString(R.string.export_failure_missing_sd));
			Log.d(TAG, context.getResources().getString(R.string.export_failure_missing_sd));
			return;
		}
		// open a file on the SD card under some useful name, and write out
		FileOutputStream fw = null;
		File f = null;
		try {
			f = new File(Environment.getExternalStorageDirectory() + "/" + Constants.EXPORT_DIR);

			f.mkdirs();
			f = new File(Environment.getExternalStorageDirectory() + "/" +  Constants.EXPORT_DIR + "/" + fileName);

			if (!f.createNewFile()) {
				f.delete();				
			}

			fw = new FileOutputStream(f);
			fw.write(data);			

		} catch (IOException e) {
			//showOutcomeDialog(R.string.export_failure_title, context.getResources().getString(R.string.export_failure_io_error));
			Log.e(TAG, context.getResources().getString(R.string.export_failure_io_error));
			return;
		} finally {
			if (fw != null) {
				try {
					fw.close();
				} catch (IOException ex) {
				}
			}
		}

		if (f == null) {
			//showOutcomeDialog(R.string.export_failure_title,context.getResources().getString(R.string.export_failure_io_error));
			Log.e(TAG, context.getResources().getString(R.string.export_failure_io_error));
			return;
		}
		// Victory! Tell the user and display the filename just written.
		//showOutcomeDialog(R.string.export_success_title, String.format(context.getResources().getString(R.string.export_success), f.getName()));
		Log.d(TAG, String.format(context.getResources().getString(R.string.export_success), f.getName()));
	}

}
